﻿$(function () {
    var url = new Url();
    var siteMessage = new SiteMessage();

    //修改时绑定页面
    var $Id = url.GetQueryString("id");
    siteMessage.BindEditPage($Id);//绑定
    //保存
    $(".btnSave").bind("click", function () {
        var $OptId = $(".hdUserId").val();
        var $MsgTitle = $(".txtMsgTitle").val();
        var $MessageContent = $(".txtMessageContent").val();
        if ($MsgTitle == null || $MsgTitle == "" || $OptId == "") {
            $(".txtMsgTitle").focus();
            $(".errorMsgTitle").text("信息标题不能为空");
            return;
        }
        if ($Id == null) {//新增
            siteMessage.Add($MsgTitle, $MessageContent, $OptId);
        }
        else {//修改
            siteMessage.Edit($Id, $MsgTitle, $MessageContent, $OptId);
        }
    });
    //搜索
    $(".btnSearch").bind("click", function () {
        var $userName = $(".txtName").val();
        var $loginName = $(".txtLoginName").val();
        $.ajax({
            type: "get",
            url: "../Handler/Operator.ashx",
            dataType: "json",
            data: { UserName: $userName, LoginName: $loginName, type: 6 },
            complete: function () {
            },
            success: function (result) {
                if (result.Status) {
                    var str = "";
                    $(result.Data).each(function (i, e) {
                        str += '<tr style="cursor:pointer;"';
                        str += 'onclick="getUserInfo(' + e.OptId + ',\'' + e.Name + '\',\'' + e.LoginName + '\');"';
                        str += 'data-dismiss="modal"><td>' + e.OptId + '</td><td>' + e.Name + '</td><td>' + e.LoginName + '</td></tr>';
                    });
                    $(".userinfo").html(str);
                } else {
                    alert(result.Message);
                }
            }
        });
    });

    $(".txtMsgTitle").change(function () {
        $(".errorMsgTitle").text("");
    });
    //去消
    $(".btnCancle").bind("click", function () {
        var url = new Url();
        var cssid = url.GetQueryString("css");
        location.href = "SiteMessageList.aspx?css=" + cssid;
    });

    $(".btnShow").bind("click", function () {
        $(".txtName").val($(".txtOptId").val());
    });

    $(".txtOptId").bind("click", function () {
        $(".btnShow").click();
    });
});
//获取用户信息
function getUserInfo(id, username, loginname) {
    $(".hdUserId").val(id);
    $(".txtOptId").val(username);
}

var SiteMessage = function () { };

SiteMessage.prototype = {
    BindEditPage: function ($Id) {
        //绑定页面数据
        if ($Id != null) {
            $.ajax({
                type: "post",
                url: "../Handler/SiteMessageHandler.ashx",
                dataType: "json",
                data: { Id: $Id, type: 4 },
                cache: false,
                complete: function () {
                },
                success: function (data) {
                    $(".txtMsgTitle").val(data.MsgTitle);
                    $(".txtMessageContent").val(data.MessageContent);
                }
            });
        }
    },
    Add: function ($MsgTitle, $MessageContent, $OptId) {
        //新增
        $.ajax({
            type: "post",
            url: "../Handler/SiteMessageHandler.ashx",
            dataType: "json",
            data: { MsgTitle: $MsgTitle, MessageContent: $MessageContent, OptId: $OptId, type: 1 },
            complete: function () {
            },
            success: function (data) {
                if (data > 0) {
                    alert("保存成功！");
                    location.href = "SiteMessageList.aspx";
                }
                else {
                    alert("保存失败！");
                }
            }
        });
    },
    Edit: function ($Id, $MsgTitle, $MessageContent, $OptId) {
        //修改
        $.ajax({
            type: "post",
            url: "../Handler/SiteMessageHandler.ashx",
            dataType: "json",
            data: { Id: $Id, MsgTitle: $MsgTitle, MessageContent: $MessageContent, OptId: $OptId, type: 2 },
            complete: function () {
            },
            success: function (data) {
                if (data > 0) {
                    alert("保存成功！");
                    location.href = "SiteMessageList.aspx";
                }
                else {
                    alert("保存失败！");
                }
            }
        });
    }
}